﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ZLDC.Common.BaseUser;
using ZLDC.DAL.Estate;
using ZLDC.Common.Estate;

namespace ZLDC.Topo.BLL.Estate
{
    public class UserBLL
    {
        public static void GetUserInfo(int id, ref string tel, ref string mobile)
        {
            DataTable dt = ZLDC.DAL.Estate.UserDAL.GetUserInfoById(id);

            if (dt != null && dt.Rows.Count > 0)
            {
                tel = dt.Rows[0]["tel"].ToString();
                mobile = dt.Rows[0]["curtMobile"].ToString();
            }
        }

        public static string Login(string user, string passWord, ref Param m_Param)
        {
            string fgs = string.Empty;
            m_Param = new Param();
            DataTable dt = ZLDC.DAL.Estate.UserDAL.GetUserInfoFromEHRNew(user, passWord);
            if (dt != null && dt.Rows.Count > 0)
            {
                fgs = "深圳"; //dt.Rows[0]["fgs"].ToString();
                string id = dt.Rows[0]["id"].ToString();
                string userNo = dt.Rows[0]["no"].ToString();
                string identityNo = dt.Rows[0]["identityNo"].ToString();
                string userName = dt.Rows[0]["name"].ToString();
                string station = dt.Rows[0]["station"].ToString();
                //string status = dt.Rows[0]["status"].ToString(); 
                string tel = dt.Rows[0]["tel"].ToString();
                string namepy = dt.Rows[0]["namepy"].ToString();
                string curtMobile = dt.Rows[0]["curtmobile"].ToString();
                string t_id = dt.Rows[0]["dept_id"].ToString();
                string pwd = dt.Rows[0]["pwd"].ToString();
                string fuuid = string.Empty;
                string lproleid = dt.Rows[0]["role_id"].ToString();
                string deptName = dt.Rows[0]["deptName"].ToString();
                string no = dt.Rows[0]["no"].ToString();

                m_Param.m_sEHRUserID = id;
                m_Param.m_sUserName = userName;
                m_Param.m_sDeptID = t_id;
                m_Param.m_sDeptName = deptName;
                m_Param.m_sStation = station;
                m_Param.m_sFgs = fgs;
                //m_Param.m_sStatus = status; 
                m_Param.m_sTel = tel; 
                m_Param.m_sNo = no;
                m_Param.m_fhName = "";
                m_Param.m_sChildrenDeptList = UserInfo.getAllChildDept(t_id, id);

                //string ehrUserId = "0";
                //string lpstation = station;
                //string lproleid = "0";
                //string lpUserId = UserDAL.GetUserIDFromEstate(identityNo, out ehrUserId, out lpstation, out lproleid); if (lpUserId.Equals("0"))
                //{
                //    pwd = DESEncryptor.Encript(pwd);
                //    lpUserId = UserDAL.InsertUserToEstate(id, userNo, identityNo, userName, station, namepy, tel, curtMobile, t_id, pwd, fuuid, deptName);

                //    UserDAL.GetUserIDFromEstate(identityNo, out ehrUserId, out lpstation, out lproleid);
                //}
                //else
                //{
                //    UserDAL.UpdateUserForEstate(lpUserId, id, userNo, identityNo, userName, station, namepy, tel, curtMobile, t_id, pwd, fuuid, deptName);
                //}
                m_Param.m_sUserID = id;
                m_Param.m_sRoleID = lproleid;

                //if (lpUserId.Equals("0"))
                //    _Utility.GoError("当前用户身份证已变更,请联系系统管理员!");
            }
            return fgs;
        }

        public static string Login(string no, string user, string passWord, string deptName, ref Param m_Param)
        {
            string fgs = string.Empty;
            m_Param = new Param();
            DataTable dt = ZLDC.DAL.Estate.UserDAL.GetUserInfoFromEHR(no, user, passWord);
            if (dt != null && dt.Rows.Count > 0)
            {
                fgs = "深圳"; //dt.Rows[0]["fgs"].ToString();
                string id = dt.Rows[0]["id"].ToString();
                string userNo = dt.Rows[0]["no"].ToString();
                string identityNo = dt.Rows[0]["identityNo"].ToString();
                string userName = dt.Rows[0]["name"].ToString();
                string station = dt.Rows[0]["station"].ToString();
                //string status = dt.Rows[0]["status"].ToString(); 
                string tel = dt.Rows[0]["tel"].ToString(); 
                string namepy = dt.Rows[0]["namepy"].ToString();
                string curtMobile = dt.Rows[0]["curtmobile"].ToString();
                string t_id = dt.Rows[0]["dept_id"].ToString();
                string pwd = dt.Rows[0]["pwd"].ToString();
                string fuuid = string.Empty;
                string lproleid = dt.Rows[0]["role_id"].ToString();

                m_Param.m_sEHRUserID = id;
                m_Param.m_sUserName = userName;
                m_Param.m_sDeptID = t_id;
                m_Param.m_sDeptName = deptName;
                m_Param.m_sStation = station;
                m_Param.m_sFgs = fgs;
                //m_Param.m_sStatus = status; 
                m_Param.m_sTel = tel; m_Param.m_sNo = no;
                m_Param.m_fhName = "";
                m_Param.m_sChildrenDeptList = UserInfo.getAllChildDept(t_id, id);

                //string ehrUserId = "0";
                //string lpstation = station;
                //string lproleid = "0";
                //string lpUserId = UserDAL.GetUserIDFromEstate(identityNo, out ehrUserId, out lpstation, out lproleid); if (lpUserId.Equals("0"))
                //{
                //    pwd = DESEncryptor.Encript(pwd);
                //    lpUserId = UserDAL.InsertUserToEstate(id, userNo, identityNo, userName, station, namepy, tel, curtMobile, t_id, pwd, fuuid, deptName);

                //    UserDAL.GetUserIDFromEstate(identityNo, out ehrUserId, out lpstation, out lproleid);
                //}
                //else
                //{
                //    UserDAL.UpdateUserForEstate(lpUserId, id, userNo, identityNo, userName, station, namepy, tel, curtMobile, t_id, pwd, fuuid, deptName);
                //}
                m_Param.m_sUserID = id;
                m_Param.m_sRoleID = lproleid;

                //if (lpUserId.Equals("0"))
                //    _Utility.GoError("当前用户身份证已变更,请联系系统管理员!");
            }
            return fgs;
        }


        public static string Login(string no, string user, string deptName, ref Param m_Param)
        {
            string fgs = string.Empty;
            m_Param = new Param();
            DataTable dt = ZLDC.DAL.Estate.UserDAL.GetUserInfoFromEHR(no, user);
            if (dt != null && dt.Rows.Count > 0)
            {
                fgs = dt.Rows[0]["fgs"].ToString();
                string id = dt.Rows[0]["id"].ToString();
                string userNo = dt.Rows[0]["no"].ToString();
                string identityNo = dt.Rows[0]["identityNo"].ToString();
                string userName = dt.Rows[0]["name"].ToString();
                string station = dt.Rows[0]["station"].ToString();
                string status = dt.Rows[0]["status"].ToString(); string tel = dt.Rows[0]["tel"].ToString(); string namepy = dt.Rows[0]["namepy"].ToString();
                string curtMobile = dt.Rows[0]["curtMobile"].ToString();
                string t_id = dt.Rows[0]["t_id"].ToString();
                string pwd = dt.Rows[0]["pwd"].ToString();
                string fuuid = string.Empty;

                m_Param.m_sEHRUserID = id;
                m_Param.m_sUserName = userName;
                m_Param.m_sDeptID = t_id;

                m_Param.m_sDeptName = deptName;
                m_Param.m_sStation = station;
                m_Param.m_sFgs = fgs;
                m_Param.m_sStatus = status; m_Param.m_sTel = tel; m_Param.m_sNo = no;
                m_Param.m_fhName = "";
                m_Param.m_sChildrenDeptList = UserInfo.getAllChildDept(t_id, id);

                string ehrUserId = "0";
                string lpstation = station;
                string lproleid = "0";
                string lpUserId = UserDAL.GetUserIDFromEstate(identityNo, out ehrUserId, out lpstation, out lproleid); if (lpUserId.Equals("0"))
                {
                    pwd = DESEncryptor.Encript(pwd);
                    lpUserId = UserDAL.InsertUserToEstate(id, userNo, identityNo, userName, station, namepy, tel, curtMobile, t_id, pwd, fuuid, deptName);

                    UserDAL.GetUserIDFromEstate(identityNo, out ehrUserId, out lpstation, out lproleid);
                }
                else
                {
                    UserDAL.UpdateUserForEstate(lpUserId, id, userNo, identityNo, userName, station, namepy, tel, curtMobile, t_id, pwd, fuuid, deptName);
                }
                m_Param.m_sUserID = lpUserId;
                m_Param.m_sRoleID = lproleid;
                if (m_Param.m_sDeptID == "805")
                {
                    m_Param.m_sDeptID = "4481";
                }
                if (lpUserId.Equals("0"))
                    _Utility.GoError("当前用户身份证已变更,请联系系统管理员!");
            }
            return fgs;
        }


        #region 楼盘自定义用户表操作

        public static ZLDC.Topo.Model.Estate.UserInfo GetUserInfoByUserID(int userId)
        {
            if (userId <= 0)
                return null;

            using (_Database db = new _Database("estate"))
            {
                return UserDAL.GetUserInfoByUserID(userId, db);
            }
        }

        public static ZLDC.Topo.Model.Estate.UserInfo GetUserInfoByUserNo(string userNo)
        {
            if (userNo.Equals(""))
                return null;

            using (_Database db = new _Database("estate"))
            {
                return UserDAL.GetUserInfoByUserNo(userNo, db);
            }
        }

        public static ZLDC.Topo.Model.Estate.UserInfo GetUserInfoByUserName(string userName, string no)
        {
            if (userName.Equals(""))
                return null;

            using (_Database db = new _Database("estate"))
            {
                return UserDAL.GetUserInfoByUserName(userName, no, db);
            }
        }

        public static ZLDC.Topo.Model.Estate.UserInfo GetUserInfoBysUserName(string sUserName)
        {
            ZLDC.Topo.Model.Estate.UserInfo info = new ZLDC.Topo.Model.Estate.UserInfo();
            try
            {
                string userNo = "0";
                int start = sUserName.IndexOf('[');
                int end = sUserName.IndexOf(',');
                userNo = sUserName.Substring(start + 1, end - start - 1);
                info = GetUserInfoByUserNo(userNo);
            }
            catch
            {
            }
            return info;
        }

        public static int GetUserIDByUserNo(string userNo)
        {
            if (userNo.Equals(""))
                return 0;

            int userId = 0;
            ZLDC.Topo.Model.Estate.UserInfo info = GetUserInfoByUserNo(userNo);

            if (info != null && info.Id > 0)
                userId = info.Id;

            return userId;
        }

        public static int GetUserIDByUserName(string userName, string no)
        {
            if (userName.Equals(""))
                return 0;

            int userId = 0;
            ZLDC.Topo.Model.Estate.UserInfo info = GetUserInfoByUserName(userName, no);

            if (info != null && info.Id > 0)
                userId = info.Id;

            return userId;
        }


        public static int GetEHRUserIDByUserNo(string userNo)
        {
            if (userNo.Equals(""))
                return 0;

            int userId = 0;
            ZLDC.Topo.Model.Estate.UserInfo info = GetUserInfoByUserNo(userNo);

            if (info != null && info.Usr != "")
                userId = _Convert.ToInt(info.Usr, 0);

            return userId;
        }

        public static string UpdateZlhqPwd(int userId, string pwd, string userStatus)
        {
            bool flag = false;

            string outmsg = "";

            flag = ZLDC.DAL.Estate.UserDAL.UpdateZlhqPwd(userId, pwd, userStatus);

            if (flag == true)
                outmsg = pwd;
            else

                outmsg = "";

            return outmsg;

        }

        public static string GetUserListByDeptid(string deptid)
        {
            string strReturn = "";
            DataSet ds = UserDAL.GetEstateUserListByDeptid(deptid);

            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i == 0)
                        strReturn = dt.Rows[i]["ID"].ToString();
                    else
                        strReturn += "," + dt.Rows[i]["ID"].ToString();
                }
            }

            return strReturn;
        }

        #endregion

        public static string p_v_usr_link_list_table(int page, int pagesize, string condition)
        {
            DataTable dt = null;
            int total = 0; int pageall = 0;
            using (_Database db = new _Database("estate"))
            {
                dt = ZLDC.DAL.Estate.UserDAL.p_v_usr_link_list_table(page, pagesize, condition, ref total, db);
            }

            pageall = _Utility.GetPageAll(pagesize, total);
            _Json json = new _Json(dt);
            return "{page:{currentpage:'" + page + "',pagesize:'" + pagesize + "',total:'" + total + "',pageall:'" + pageall + "'},record:" + json.ToJson() + "}";
        }


    }
}
